package com.wyd.tool;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ModelCreate {

    public static final String PACKAGE_KEY = "";

    // 模块对应表
    public static final Map<String, String> hashMap = new HashMap<>();
    public static final Map<String, String> namespaceMap = new HashMap<>();

    static {
		hashMap.put("tc_product", PACKAGE_KEY);

        namespaceMap.put(PACKAGE_KEY, "com.wyd.");
    }

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://47.106.177.244:3305/study?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&verifyServerCertificate=false&useSSL=false", "root", "wyd@mysql");
            List<String> tableNames = DBTableUtil.getTableNames(conn, "study");

            if (!tableNames.isEmpty()) {
                // 循环表内容
                List<TableNode> tableNodes = new ArrayList<>();
                for (int j = 0; j < tableNames.size(); j++) {
                    if (!hashMap.containsKey(tableNames.get(j))) {
                        System.out.println("未找对对应表：" + tableNames.get(j));
                        continue;
                    }
                    TableNode tableNode = new TableNode();
                    tableNodes.add(tableNode);
                    tableNode.setTableName(tableNames.get(j));
                    tableNode.setTableOrdinaryName(tableNames.get(j));
                    tableNode.setNamespace(namespaceMap.get(hashMap.get(tableNames.get(j))));

                    List<TableNode.Node> nodes = tableNode.getNodes();
                    if (nodes == null) {
                        nodes = new ArrayList<>();
                        tableNode.setNodes(nodes);
                    }
                    ResultSet columnSets = null;
                    String primaryKey = "";
                    try {
                        columnSets = conn.getMetaData().getPrimaryKeys("study", "%", tableNames.get(j));
                        while (columnSets.next()) {
                            primaryKey = columnSets.getString(4);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (columnSets != null) {
                            columnSets.close();
                        }
                    }

                    ResultSet columnSet = null;
                    try {
                        columnSet = conn.getMetaData().getColumns("study", "%", tableNames.get(j), "%");
                        while (columnSet.next()) {
                            TableNode.Node node = new TableNode.Node();
                            String ordinaryName = columnSet.getString("COLUMN_NAME");
                            if (ordinaryName.equalsIgnoreCase(primaryKey)) {
                                node.setIsPrimaryKey("0");
                            }
                            node.setFieldName(ordinaryName);
                            node.setOrdinaryName(ordinaryName);
                            node.setFieldType(columnSet.getString("TYPE_NAME"));
                            node.setRemark(columnSet.getString("REMARKS"));
                            nodes.add(node);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (columnSet != null) {
                            columnSet.close();
                        }
                    }
                }

                System.out.println(JSON.toJSONString(tableNodes));
                for (int i = 0; i < tableNodes.size(); i++) {
                    Map<String, Object> tss = new HashMap<>();
                    TableNode value = tableNodes.get(i);
                    tss.put("tableNode", value);
                    if (!hashMap.containsKey(value.getTableOrdinaryName())) {
                        continue;
                    }

                    String key = hashMap.get(value.getTableOrdinaryName());

                    String path ="D:\\项目代码\\wuyongde\\spring-cloud-alibaba\\generate";

                    String namespace = value.getNamespace();
                    File myFiles = new File(path+"/src/main/java/" + namespace.replace(".", "/") + "interfaces/"
                            + TableNode.captureName(value.getTableName()) + "Service.java");
                    MyTemplate myTemplates = new MyTemplate(tss, path+"/src/main/resources/template/", "interface.flt",
                            myFiles);
                    myTemplates.generat(false);

                    myFiles = new File(path+"/src/main/java/" + namespace.replace(".", "/") + "service/"
                            + TableNode.captureName(value.getTableName()) + "ServiceImpl.java");
                    myTemplates = new MyTemplate(tss, path+"/src/main/resources/template/", "service.flt",
                            myFiles);
                    myTemplates.generat(false);

                    myFiles = new File(path+"/src/main/java/" + namespace.replace(".", "/") + "entity/"
                            + TableNode.captureName(value.getTableName()) + ".java");
                    myTemplates = new MyTemplate(tss, path+"/src/main/resources/template/", "entities.flt",
                            myFiles);
                    myTemplates.generat(false);

                    myFiles = new File(path+"/src/main/java/" + namespace.replace(".", "/") + "mapper/"
                            + TableNode.captureName(value.getTableName()) + "Mapper.java");
                    myTemplates = new MyTemplate(tss, path+"/src/main/resources/template/", "mapper.flt",
                            myFiles);
                    myTemplates.generat(false);

                    myFiles = new File(path+"/src/main/resources/mapper/" + key
                            + TableNode.captureName(value.getTableName()) + "Mapper.xml");
                    myTemplates = new MyTemplate(tss, path+"/src/main/resources/template/", "xml.flt",
                            myFiles);
                    myTemplates.generat(false);

                }

            }
        } catch (SQLException |

                ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
